package edu.nwpu.controller;


import com.alibaba.fastjson.JSON;
import edu.nwpu.pojo.RecvInfo;
import edu.nwpu.service.RecvInfoService;
import edu.nwpu.utils.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.BinaryOperator;

@RestController
public class RecvInfoController {

    @Resource
    private RecvInfoService recvInfoService;

    @ApiOperation(value = "添加用户地址信息", httpMethod = "POST", response = String.class,
            notes = "返回json")
    @PostMapping("/recv/add")
    public String addRecvInfo(RecvInfo recvInfo) {
        boolean status = recvInfoService.addRecvInfo(recvInfo);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @ApiOperation(value = "删除指定id的用户地址信息", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/recv/delete/recvId/{recvId}")
    public String deleteRecvInfoByRecvId(@PathVariable("recvId") Long id) {
        boolean status = recvInfoService.deleteRecvInfoById(id);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @ApiOperation(value = "更新地址信息", httpMethod = "POST", response = String.class,
            notes = "返回json")
    @PostMapping("/recv/update")
    public String updateRecvInfo(RecvInfo recvInfo) {
        boolean status = recvInfoService.updateRecvInfo(recvInfo);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @ApiOperation(value = "查询一个用户所有的地址信息", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/recv/query/userId/{userId}")
    public String queryRecvInfoByUserId(@PathVariable("userId") Long id) {
        List<RecvInfo> list = recvInfoService.getRecvInfoByUserId(id);
        return JSON.toJSONString(ResponseUtil.responseQueryList(list));
    }

}
